package com.example.mystore.controller;

import com.example.mystore.model.User;
import com.example.mystore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> removeUserById(@PathVariable int id) {
        int result = userService.removeUserById(id);
        if (result > 0) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
    }

    @PostMapping("/register")
    public ResponseEntity<User> registerUser(@RequestBody User user) {
        User registeredUser = userService.register(user);
        return ResponseEntity.status(HttpStatus.CREATED).body(registeredUser);
    }

    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> loginUser(@RequestBody Map<String, String> credentials) {
        String account = credentials.get("account");
        String password = credentials.get("password");
        User loggedInUser = userService.login(account, password);

        if (loggedInUser != null) {
            Map<String, Object> response = Map.of(
                    "id", loggedInUser.getId(),
                    "account", loggedInUser.getAccount(),
                    "password", loggedInUser.getPassword(),
                    "role", loggedInUser.getRole()
            );
            return ResponseEntity.ok(response);
        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        }
    }
    @PostMapping("/forgot-password")
    public ResponseEntity<?> forgotPassword(@RequestBody Map<String, String> credentials) {
        String account = credentials.get("account");
        String phone = credentials.get("phone");
        String email = credentials.get("email");
        String question = credentials.get("question");
        String answer = credentials.get("answer");

        boolean isVerified = userService.validateUserInfo(account, phone, email, question, answer);

        if (isVerified) {
            return ResponseEntity.ok(Map.of("message", "验证成功"));
        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Map.of("message", "验证失败"));
        }
    }
    @PatchMapping("/change-password")
    public ResponseEntity<Void> changePassword(@RequestBody Map<String, String> passwordInfo) {
        String account = passwordInfo.get("account");
        String newPassword = passwordInfo.get("newPassword");

        boolean updated = userService.updatePassword(account, newPassword);
        if (updated) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
    @PatchMapping("/{id}")
    public ResponseEntity<User> updateUser(@PathVariable int id, @RequestBody User user) {
        System.out.println("Received update request: " + user);
        user.setId(id);
        user.setRole(user.getRole());
        User updatedUser = userService.updateUser(user);
        if (updatedUser != null) {
            return ResponseEntity.ok(updatedUser);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/{id}")
    public User getUserById(@PathVariable int id) {
        return userService.getUserById(id);
    }

    @GetMapping
    public ResponseEntity<List<User>> getAllUsers() {
        List<User> users = userService.getAllUsers();
        return ResponseEntity.ok(users);
    }
    @PatchMapping("/{id}/updatePassword")
    public ResponseEntity<Void> updatePassword(@PathVariable("id") Integer id,
                                               @RequestParam("asw") String asw,
                                               @RequestParam("newPassword") String newPassword) {
        boolean updated = userService.updatePassword(id, asw, newPassword);
        if (updated) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
    }

}
